import {_decorator, Button, Component, director, instantiate, Node, Prefab, Sprite, UITransform, Vec3} from 'cc';
import {NormalBuilding} from "db://assets/script/game/city/normal-building";
import {GameEvent} from "db://assets/script/game-event";
import {UserFishFryListEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-list-event";
import {UserFishFryListLoadedEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-list-loaded-event";
import {FishFryInCity} from "db://assets/script/game/fish/fish-fry-in-city";
import {RandomMapAssets} from "db://assets/script/game/world/random-map-assets";
import {UserFishFryComeEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-come-event";

const {ccclass, property} = _decorator;

@ccclass('FishFryTop')
export class FishFryTop extends Component {

    @property(Prefab)
    normalBackground!: Prefab;

    @property(Node)
    fishFryBase!: Node

    @property(Node)
    fishFryBox!: Node

    @property(Node)
    fishFryBoxGround!: Node


    @property(RandomMapAssets)
    randomMapAssets!: RandomMapAssets;

    @property(Button)
    upgradeBtn!: Button

    @property(Prefab)
    fishFryPrefab!: Prefab

    building!: NormalBuilding

    groundCeil: Array<Array<Node>> = []
    baseGroundHeight = 10
    baseGroundWidth = 0

    init(building: NormalBuilding) {
        this.building = building;
        this.upgradeBtn.node.on(Button.EventType.CLICK, building.upgrade, building)
        this.upgradeBtn.node.active = this.building.upgradeButton.node.active
        this.appendCeil(30)
        director.emit(GameEvent.userFishRequestFishFryCome, new UserFishFryComeEvent())
        this.onEvent()
    }

    appendCeil(columns: number) {
        const xOffset = this.groundCeil.length
        for (let x = 0; x < columns; x++) {
            const ceilArray: Node[] = []
            for (let y = 0; y < this.baseGroundHeight; y++) {
                const ceilNode = this._randomOneCeil()
                ceilArray.push(ceilNode)
                this.fishFryBoxGround.addChild(ceilNode)
                ceilNode.setPosition((xOffset + x) * 32, -y * 32)
            }
            this.groundCeil.push(ceilArray)
        }
        this.baseGroundWidth += columns
    }

    onEvent() {
        director.on(GameEvent.userFishFryComeLoaded, this.fryLoaded, this)
    }

    offEvent() {
        director.off(GameEvent.userFishFryComeLoaded, this.fryLoaded, this)
    }

    fryLoaded(event: UserFishFryListLoadedEvent) {
        this.fishFryBox.destroyAllChildren()
        let groundWidth = 0
        for (let i = 0; i < event.list.length; i++) {
            const fishFry = event.list[i]
            const ffn = instantiate(this.fishFryPrefab)
            this.fishFryBox.addChild(ffn)
            ffn.getComponent(FishFryInCity)!.init(fishFry)
            const width = ffn.getComponent(UITransform)!.width
            const height = ffn.getComponent(UITransform)!.height
            groundWidth += width * 0.9
            ffn.setPosition(new Vec3(30 + width * 0.9 * i, -(30 + height + (i % 2) * height)))
        }
        this.appendCeil(Math.round((groundWidth + 60) / 32) - this.baseGroundWidth + 3)
        this.fishFryBase.getComponent(UITransform)!.width = groundWidth + 60
        director.emit(GameEvent.userFishFrySee, 1)
    }

    protected onDestroy() {
        this.offEvent()
    }

    // 实例化一个随机单元格
    private _randomOneCeil() {
        const ground = instantiate(this.normalBackground)
        ground.getComponent(Sprite)!.spriteFrame = this.randomMapAssets.randomCeil()
        ground.layer = this.node.layer
        return ground
    }

}
